import axios from 'axios';

// 修改为8081端口
const API_BASE_URL = 'http://localhost:8081/api';

// 创建axios实例
const apiClient = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 请求拦截器，可以在这里添加token等鉴权信息
apiClient.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token');
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 响应拦截器，统一处理错误
apiClient.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    const message = error.response?.data || '服务器错误';
    console.error('API错误:', message);
    return Promise.reject(message);
  }
);

// 用户相关API
export const userApi = {
  register: (userData) => apiClient.post('/users/register', userData),
  login: (credentials) => apiClient.post('/users/login', credentials),
  getUserInfo: (userId) => apiClient.get(`/users/${userId}`),
  updateUserInfo: (userId, userData) => apiClient.put(`/users/${userId}`, userData)
};

// 日程相关API
export const scheduleApi = {
  getAllSchedules: (userId) => apiClient.get(`/schedules/user/${userId}`),
  getSchedulesByDate: (userId, date) => apiClient.get(`/schedules/user/${userId}/date/${date}`),
  getSchedulesByDateRange: (userId, startDate, endDate) => 
    apiClient.get(`/schedules/user/${userId}/dateRange?startDate=${startDate}&endDate=${endDate}`),
  getScheduleById: (scheduleId) => apiClient.get(`/schedules/${scheduleId}`),
  createSchedule: (userId, scheduleData) => apiClient.post(`/schedules/user/${userId}`, scheduleData),
  updateSchedule: (scheduleId, scheduleData) => apiClient.put(`/schedules/${scheduleId}`, scheduleData),
  completeSchedule: (scheduleId) => apiClient.put(`/schedules/${scheduleId}/complete`),
  deleteSchedule: (scheduleId) => apiClient.delete(`/schedules/${scheduleId}`)
};

// 积分相关API
export const pointsApi = {
  getPoints: (userId) => apiClient.get(`/points/user/${userId}`),
  getPointsHistory: (userId) => apiClient.get(`/points/user/${userId}/history`),
  getRewards: (userId) => apiClient.get(`/points/user/${userId}/rewards`),
  getPenalties: (userId) => apiClient.get(`/points/user/${userId}/penalties`),
  createReward: (userId, rewardData) => apiClient.post(`/points/user/${userId}/rewards`, rewardData),
  createPenalty: (userId, penaltyData) => apiClient.post(`/points/user/${userId}/penalties`, penaltyData),
  updateReward: (rewardId, rewardData) => apiClient.put(`/points/rewards/${rewardId}`, rewardData),
  updatePenalty: (penaltyId, penaltyData) => apiClient.put(`/points/penalties/${penaltyId}`, penaltyData),
  deleteReward: (rewardId) => apiClient.delete(`/points/rewards/${rewardId}`),
  deletePenalty: (penaltyId) => apiClient.delete(`/points/penalties/${penaltyId}`),
  redeemReward: (userId, rewardId) => apiClient.post(`/points/user/${userId}/rewards/${rewardId}/redeem`),
  acceptPenalty: (userId, penaltyId) => apiClient.post(`/points/user/${userId}/penalties/${penaltyId}/accept`),
  updatePointsSettings: (userId, settings) => apiClient.put(`/points/user/${userId}/settings`, settings)
};

export default {
  userApi,
  scheduleApi,
  pointsApi
}; 